﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Diagnostics;
using System.Threading;

using iPhoneUI.Shell;

namespace iPhoneUI.Widgets
{
    public class UnlockSlider : Widget
    {
        private Bitmap m_slideAnimation;
        private BitmapButton m_arrowButton;

        private bool m_locked;
        private bool m_reseting;

        private int m_leftLimit = 16;
        private int m_rightLimit = 220;

        private const int FRAME_WIDTH = 240;

        private Rectangle m_currentAnimationFrame;

        public UnlockSlider()
        {
            m_slideAnimation = ShellConfig.CreateImage("slideAni.bmp");
        }

        public string UnlockPage { get; set; }

        protected override void OnSetOwner()
        {            
            m_arrowButton = new BitmapButton();
            m_arrowButton.SetOwner(Owner);

            m_arrowButton.Image = ShellConfig.CreateImage("arrow.bmp");
            m_arrowButton.ImageDown = ShellConfig.CreateImage("arrowDown.bmp");

            SetInitialSize(new Size(Owner.Width, m_slideAnimation.Height));
            SetUpdateTimer(80);
 
            // compute actual left and right limits as a ratio of the design time values
            m_leftLimit = Owner.ScaleX(16);
            m_rightLimit = Owner.ScaleX(220);
            
            base.OnSetOwner();
        }

        protected override void Layout()
        {
            m_arrowButton.Location = new Point(m_leftLimit, ClientArea.Y + Owner.ScaleY(16));

            m_currentAnimationFrame = Rectangle.FromLTRB(0, 0, FRAME_WIDTH, m_slideAnimation.Height);

            base.Layout();
        }

        public override void Resume()
        {
            Locked = false;
            base.Resume();
        }

        public override void OwnerMouseMove(int x, int y)
        {
            if (m_arrowButton.Pushed)
                Move(x);
        }

        public override void OwnerMouseDown(int x, int y)
        {
            if (HitTest(x, y))
                m_arrowButton.OwnerMouseDown(x, y);
        }

        public override void OwnerMouseUp(int x, int y)
        {
            ResetPosition();
        }

        private void Move(int x)
        {
            int newX = CenterOn(m_arrowButton.ClientArea.Width, x);

            if (newX <= m_leftLimit)
            {
                newX = m_leftLimit;
                m_reseting = false;
            }

            if (newX + m_arrowButton.ClientArea.Width >= m_rightLimit)
            {
                newX = m_rightLimit - m_arrowButton.ClientArea.Width;
                Locked = true;
            }

            m_arrowButton.Location = new Point(newX, m_arrowButton.ClientArea.Y);
            Update();
        }

        public bool Locked
        {
            get
            {
                return m_locked;
            }
            set
            {
                if (m_locked != value)
                {
                    m_locked = value;

                    if (m_locked == false)
                        ResetPosition();
                    else
                        OnUnlock();
                }
            }
        }

        public void ResetPosition()
        {
            m_arrowButton.Pushed = false;
            m_reseting = true;
        }

        protected override void OnDraw(Canvas canvas, Rectangle clipBounds)
        {
            m_currentAnimationFrame.X += FRAME_WIDTH;
            if (m_currentAnimationFrame.X >= m_slideAnimation.Width)
                m_currentAnimationFrame.X = 0;

            canvas.AlphaBlend(m_slideAnimation, 160, ClientArea, m_currentAnimationFrame);
            
            if (m_reseting)
                Move(m_arrowButton.ClientArea.X - 15);

            m_arrowButton.Draw(canvas, clipBounds);
        }

        protected virtual void OnUnlock()
        {
            Owner.GotoPage(UnlockPage);
        }

        protected override void OnDispose()
        {
            m_slideAnimation.SafeDispose();
            m_arrowButton.SafeDispose();
        }
    }
}
